<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button icon="search" type="default" slot="title" round to="/search">搜索</van-button>
    </van-nav-bar>
    <van-tabs v-model="active" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 频道的文章列表 -->
        <article-list :channel="item"></article-list>
      </van-tab>
      <template #nav-right>
        <div class="placeholder"></div>
        <div class="more" @click="isChannelShow = true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <!-- 自定义图标弹出层 -->
    <van-popup v-model="isChannelShow" closeable close-icon="close" position="bottom" :style="{ height: '100%' }">
      <channel-edit :myChannels="channels" :active="active" @update-active="updateActive"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getChannelList } from '@/api/user'
import ArticleList from './components/article-list' // 文章列表组件
import ChannelEdit from './components/channel-edit' // 弹出层频道编辑子组件
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomePage',
  // 注册子组件
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      isChannelShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannelList()
  },
  mounted () { },
  methods: {
    async loadChannelList () {
      try {
        var list = getItem('TOUTIAO_CHANNELS')
        // 用户没有登录并且本地有数据的时候就用本地数据
        if (!this.user && list) {
          this.channels = list
        } else {
          // 否则用线上的接口
          const { data: res } = await getChannelList()
          this.channels = res.data.channels
          // console.log(this.channels)
        }
      } catch (err) {
        this.$toast.fail('获取频道数据失败！')
      }
    },
    // 频道列表点击选择频道
    updateActive (index, flag = false) {
      this.active = index
      this.isChannelShow = flag
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  .page-nav-bar {
    /deep/.van-nav-bar__title {
      max-width: unset;
    }
    .van-button {
      width: 555px;
      height: 64px;
      border: none;
      color: #fff;
      background-color: #5babfb;
      font-size: 27px;
      .van-icon {
        color: #fff;
        font-size: 32px;
      }
    }
  }
  /deep/ .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
  }
  /deep/ .van-tabs__line {
    width: 31px;
    height: 6px;
    background-color: #3296fa;
  }
  .more {
    position: fixed;
    right: 0;
    width: 66px;
    height: 82px;
    background-color: rgba(255, 255, 255, 0.9);
    text-align: center;
    line-height: 82px;
    .toutiao {
      font-size: 33px;
    }
    &::before {
      content: '';
      display: block;
      position: absolute;
      left: 0;
      width: 4px;
      height: 82px;
      background: url(~@/assets/gradient-gray-line.png) no-repeat;
      background-size: contain;
    }
  }
  // 权重：min-width(max-width)>flex-basis>width
  .placeholder {
    min-width: 66px;
    height: 82px;
  }
  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
  }
  /deep/ .van-tabs__content {
    padding-top: 174px;
  }
}
</style>
